﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpeedTest
{
	public partial class PreferencesForm : Form
	{
        private Data data = null;

		public PreferencesForm(Data data)
		{
			InitializeComponent();

            if (data == null) { groupSTPassword.Enabled = false; }
            else { this.data = data; }
		}

		private void PreferencesForm_Load(object sender, EventArgs e)
		{
			EnableInput(false);

			LoadPreferences();
		}

		private void LoadPreferences()
		{
			LoadColorOptions();
            LoadAppOptions();
		}

		private void LoadColorOptions()
		{
			//FAILED COLOR
			if (Properties.Settings.Default.FailColor != null)
			{
				btnFailedColor.BackColor = Properties.Settings.Default.FailColor;
			}
			else { btnFailedColor.BackColor = Color.Red; }

			//PASSED COLOR
			if (Properties.Settings.Default.PassColor != null)
			{
				btnPassedColor.BackColor = Properties.Settings.Default.PassColor;
			}
			else { btnPassedColor.BackColor = Color.Green; }

			//BLOCKED COLOR
			if (Properties.Settings.Default.BlockedColor != null)
			{
				btnBlockedColor.BackColor = Properties.Settings.Default.BlockedColor;
			}
			else { btnBlockedColor.BackColor = Color.Yellow; }

			//RESCHEDULED COLOR
			if (Properties.Settings.Default.RescheduledColor != null)
			{
				btnRescheduledColor.BackColor = Properties.Settings.Default.RescheduledColor;
			}
			else { btnRescheduledColor.BackColor = Color.Gray; }

			//UNTESTABLE COLOR
			if (Properties.Settings.Default.UntestableColor != null)
			{
				btnUntestableColor.BackColor = Properties.Settings.Default.UntestableColor;
			}
			else { btnUntestableColor.BackColor = Color.Gray; }

			//USE COLOR?
			chkUseColors.Checked = Properties.Settings.Default.UseColors;

			//NAVIGATION TRANSPARENCY
			if (Properties.Settings.Default.NavColorTransparency > 0)
			{
				spinNavPercent.Value = Properties.Settings.Default.NavColorTransparency;
			}
			else { spinNavPercent.Value = 50; }
		}

		private void SaveColorOptions()
		{
			Properties.Settings.Default.FailColor = btnFailedColor.BackColor;
			Properties.Settings.Default.PassColor = btnPassedColor.BackColor;
			Properties.Settings.Default.BlockedColor = btnBlockedColor.BackColor;
			Properties.Settings.Default.RescheduledColor = btnRescheduledColor.BackColor;
			Properties.Settings.Default.UntestableColor = btnUntestableColor.BackColor;
			Properties.Settings.Default.UseColors = chkUseColors.Checked;
			Properties.Settings.Default.NavColorTransparency = (int)spinNavPercent.Value;
		}

		private void LoadAppOptions()
		{
            txtDBServer.Text = Properties.Settings.Default.DBURL;
			txtDBPort.Text = Properties.Settings.Default.DBPort;
            txtSTURL.Text = Properties.Settings.Default.MainSiteURL;
            saveWindowSizeCheckBox.Checked = Properties.Settings.Default.SaveWindowSize;
		}

		private void SaveAppOptions()
		{
            Properties.Settings.Default.DBURL = txtDBServer.Text;
			Properties.Settings.Default.DBPort = txtDBPort.Text;
            Properties.Settings.Default.MainSiteURL = txtSTURL.Text;
            Properties.Settings.Default.SaveWindowSize = saveWindowSizeCheckBox.Checked;
		}

		private bool ValidateAppPassword()
		{

			return false;
		}

		private void EnableInput(bool enable)
		{
			groupSTServer.Enabled = enable;
			groupColors.Enabled = enable;

			btnEdit.Enabled = !enable;
			btnEdit2.Enabled = !enable;
			btnSave.Enabled = enable;
			btnSave2.Enabled = enable;
			btnCancel.Enabled = enable;
			btnCancel2.Enabled = enable;

            saveWindowSizeCheckBox.Enabled = enable;
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			EnableInput(true);
            txtDBServer.Focus();
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			SaveColorOptions();
            SaveAppOptions();
			EnableInput(false);
			Properties.Settings.Default.Save();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			LoadPreferences();
			EnableInput(false);
		}

		private void btnColor_Click(object sender, EventArgs e)
		{
			ColorDialog colorSelector = new ColorDialog();
			colorSelector.AllowFullOpen = false;
			colorSelector.FullOpen = false;
			colorSelector.SolidColorOnly = true;
			DialogResult res = colorSelector.ShowDialog();

			if (res != DialogResult.Cancel)
			{
				Button colorButton = (Button)sender;
				colorButton.BackColor = colorSelector.Color;
			}
		}

        private void btnSavePassword_Click(object sender, EventArgs e)
        {
            if (txtSTNewPassword.Text.Equals(txtSTRepeatPassword.Text))
            {
                if (!data.UpdatePassword(data.User.UserID, txtSTOldPassword.Text, txtSTNewPassword.Text))
                {
                    MessageBox.Show("Unable to change the password. Please check your entries and try again.");
                }
                else
                {
                    MessageBox.Show("Password changed.");
                }
            }
            else { MessageBox.Show("The passwords entered do not match. Please try again."); }
        }

		private void txtDBPort_KeyPress(object sender, KeyPressEventArgs e)
		{
			int parsedNumber = 0;
			e.Handled = !Int32.TryParse(e.KeyChar.ToString(), out parsedNumber);
			if (e.KeyChar == (char)Keys.Delete || e.KeyChar == (char)Keys.Back) { e.Handled = false; }
		}

	}
}
